testsuite: Rework memorytexture test some more
authorBenjamin Otte <otte@redhat.com>
Sat, 11 Sep 2021 20:26:37 +0000 (22:26 +0200)
committerBenjamin Otte <otte@redhat.com>
Sun, 12 Sep 2021 03:54:37 +0000 (05:54 +0200)
Instead of predefined colors, generate them randomly.

testsuite/gdk/memorytexture.c

index 49f71599658cf50c711bbef134600b79bd7aaa52..c3cbc71e2beae14c8b16212f0fb5db35147e2412 100644 (file)
@@ -1,33 +1,9 @@
 #include <locale.h>
 #include <gdk/gdk.h>
 
-typedef enum {
-  BLUE,
-  GREEN,
-  RED,
-  TRANSPARENT,
-  ALMOST_OPAQUE_REBECCAPURPLE,
-  N_COLORS
-} Color;
-
-static const char * color_names[N_COLORS] = {
-  "blue",
-  "green",
-  "red",
-  "transparent",
-  "almost_opaque_rebeccapurple"
-};
-
-static const GdkRGBA colors[N_COLORS] = {
-  { 0.0, 0.0, 1.0, 1.0 },
-  { 0.0, 1.0, 0.0, 1.0 },
-  { 1.0, 0.0, 0.0, 1.0 },
-  { 0.0, 0.0, 0.0, 0.0 },
-  { 0.4, 0.2, 0.6, 2.f/3.f },
-};
+#define N 50
 
 typedef struct _TextureBuilder TextureBuilder;
-typedef struct _TestData TestData;
 
 struct _TextureBuilder
 {
@@ -40,12 +16,6 @@ struct _TextureBuilder
   gsize offset;
 };
 
-struct _TestData
-{
-  GdkMemoryFormat format;
-  Color color;
-};
-
 static gsize
 gdk_memory_format_bytes_per_pixel (GdkMemoryFormat format)
 {
@@ -367,33 +337,57 @@ create_texture (GdkMemoryFormat  format,
 }
 
 static void
-test_download_1x1 (gconstpointer data)
+create_random_color (GdkRGBA *color)
 {
-  const TestData *test_data = data;
+  /* Generate colors so that premultiplying will result in values in steps of 1/15th */
+  color->red = g_test_rand_int_range (0, 6) / 5.f;
+  color->green = g_test_rand_int_range (0, 6) / 5.f;
+  color->blue = g_test_rand_int_range (0, 6) / 5.f;
+  color->alpha = g_test_rand_int_range (0, 4) / 3.f;
+}
+
+static void
+test_download_1x1 (gconstpointer format_)
+{
+  GdkMemoryFormat format = GPOINTER_TO_SIZE (format_);
   GdkTexture *expected, *test;
+  gsize i;
 
-  expected = create_texture (GDK_MEMORY_DEFAULT, 1, 1, &colors[test_data->color]);
-  test = create_texture (test_data->format, 1, 1, &colors[test_data->color]);
+  for (i = 0; i < N; i++)
+    {
+      GdkRGBA color;
 
-  compare_textures (expected, test, gdk_memory_format_has_alpha (test_data->format));
+      create_random_color (&color);
+      expected = create_texture (GDK_MEMORY_DEFAULT, 1, 1, &color);
+      test = create_texture (format, 1, 1, &color);
+      
+      compare_textures (expected, test, gdk_memory_format_has_alpha (format));
 
-  g_object_unref (expected);
-  g_object_unref (test);
+      g_object_unref (expected);
+      g_object_unref (test);
+    }
 }
 
 static void
-test_download_4x4 (gconstpointer data)
+test_download_4x4 (gconstpointer format_)
 {
-  const TestData *test_data = data;
+  GdkMemoryFormat format = GPOINTER_TO_SIZE (format_);
   GdkTexture *expected, *test;
+  gsize i;
 
-  expected = create_texture (GDK_MEMORY_DEFAULT, 4, 4, &colors[test_data->color]);
-  test = create_texture (test_data->format, 4, 4, &colors[test_data->color]);
+  for (i = 0; i < N; i++)
+    {
+      GdkRGBA color;
 
-  compare_textures (expected, test, gdk_memory_format_has_alpha (test_data->format));
+      create_random_color (&color);
+      expected = create_texture (GDK_MEMORY_DEFAULT, 4, 4, &color);
+      test = create_texture (format, 4, 4, &color);
+      
+      compare_textures (expected, test, gdk_memory_format_has_alpha (format));
 
-  g_object_unref (expected);
-  g_object_unref (test);
+      g_object_unref (expected);
+      g_object_unref (test);
+    }
 }
 
 static void
@@ -401,25 +395,17 @@ add_test (const char    *name,
           GTestDataFunc  func)
 {
   GdkMemoryFormat format;
-  Color color;
   GEnumClass *enum_class;
 
   enum_class = g_type_class_ref (GDK_TYPE_MEMORY_FORMAT);
 
   for (format = 0; format < GDK_MEMORY_N_FORMATS; format++)
     {
-      for (color = 0; color < N_COLORS; color++)
-        {
-          TestData *test_data = g_new (TestData, 1);
-          char *test_name = g_strdup_printf ("%s/%s/%s",
-                                             name,
-                                             g_enum_get_value (enum_class, format)->value_nick,
-                                             color_names[color]);
-          test_data->format = format;
-          test_data->color = color;
-          g_test_add_data_func_full (test_name, test_data, test_download_1x1, g_free);
-          g_free (test_name);
-        }
+      char *test_name = g_strdup_printf ("%s/%s",
+                                         name,
+                                         g_enum_get_value (enum_class, format)->value_nick);
+      g_test_add_data_func_full (test_name, GSIZE_TO_POINTER (format), test_download_1x1, NULL);
+      g_free (test_name);
     }
 }